﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Prism.Ioc;
using Prism.Regions;
using YJTTool.Common.Events;
using YJTTool.DBBaseServer.AbstractModel;
using YJTTool.Handy.ViewModels;
using YJTTool.Handy.Views;

namespace YJTTool.Handy.Views
{
    /// <summary>
    /// LoginView.xaml 的交互逻辑
    /// </summary>
    public partial class LoginView
    {
        private readonly IContainerExtension _container;
        private readonly IRegionManager _regionManager;
        public LoginView(IContainerExtension container, IRegionManager regionManager)
        {
            InitializeComponent();
            _container = container;
            _regionManager = regionManager;
            LoginVisibilityEvent.Instance.Subscribe(LoginCommandMethod);
            var viewmodel = new LoginViewModel();
            viewmodel.LoginSuccess += OnLoginSuccess;
            DataContext = viewmodel;
        }

        private void OnLoginSuccess()
        {
            var mainWindow = _container.Resolve<MainWindow>();
            RegionManager.SetRegionManager(mainWindow, _regionManager);
            mainWindow.Show();
            Close();
        }
        private readonly AbstractUserInfoServer abstractUserInfo;
        public LoginView()
        {
            InitializeComponent();
            abstractUserInfo = new AbstractUserInfoServer();
        }
        private void textUser_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            txtUser.Focus();

        }
        private void LoginCommandMethod(Visibility visibility)
        {
            if (visibility == Visibility.Collapsed)
            {
                GlobalLoadingMask.Visibility = Visibility.Collapsed;
            }
            else
            {
                GlobalLoadingMask.Visibility = Visibility.Visible;
            }
        }
        private void txtUser_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            if (!string.IsNullOrEmpty(txtUser.Text) && txtUser.Text.Length > 0)
            {
                textUser.Visibility = Visibility.Collapsed;
            }
            else
            {
                textUser.Visibility = Visibility.Visible;
            }
        }

        private void textPassword_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            txtPassword.Focus();
        }

        private void Border_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (e.ChangedButton == MouseButton.Left)
            {
                //this.DragMove();
            }
        }

        private void Image_MouseUp(object sender, MouseButtonEventArgs e)
        {
            this.Close();
        }

        private void txtPassword_TextChanged(object sender, RoutedEventArgs e)
        {
            if (!string.IsNullOrEmpty(txtPassword.Password) && txtPassword.Password.Length > 0)
            {
                textPassword.Visibility = Visibility.Collapsed;
            }
            else
            {
                textPassword.Visibility = Visibility.Visible;
            }
        }

    }
}
